package com.xhwl.centiotdata.pushtrigger.security;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.dto.cent.device.iot.*;
import com.xhwl.common.enums.centsecurity.PerimeterStatusEnum;
import com.xhwl.common.query.cent.device.DevicePageQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @Author: yanyixiong
 * @Description:
 * @Date: create in 9:41 2022/1/25
 */
@Slf4j
@RestController
public class PerimeterController {

    @Autowired
    private IDeviceCentService deviceCentService;

    @PostMapping("/security-perimeter/update-protection")
    public void deviceServiceContentTest(@RequestBody IotDeviceJson<DeviceServiceBody<Object>> iotDeviceJson) {

        Object properties = iotDeviceJson.getNotify_data().getBody().getServices().get(0).getProperties();
        CommonHeader header = iotDeviceJson.getNotify_data().getHeader();
        //iot设备id，格式（产品id_项目id_系统大类id_子系统id_第三方设备id）
        String iotDeviceId = header.getDevice_id();
        IotPerimeterDevice iotPerimeterDevice = JSONObject.parseObject(JSONObject.toJSONString(properties), IotPerimeterDevice.class);
        log.info("iotPerimeterDevice:{}", iotPerimeterDevice.toString());
        if (!StringUtils.isEmpty(iotPerimeterDevice.getDeployStatus())) {
            String deployStatus = iotPerimeterDevice.getDeployStatus();
            DevicePageQuery deviceQuery = new DevicePageQuery();
            deviceQuery.setIotDeviceId(iotDeviceId);
            List<DeviceDTO> deviceDTOList = deviceCentService.listDevice(null, deviceQuery);
            if (!deviceDTOList.isEmpty()) {
                DeviceDTO deviceDTO = deviceDTOList.get(0);
                JSONObject attributeJson = deviceDTO.getAttributeJson();
                if (attributeJson == null) {
                    attributeJson = new JSONObject();
                }
                //设防、旁路、 撤防
                attributeJson.put("protection", PerimeterStatusEnum.getPerimeterStatusEnum(deployStatus).getValue());
                attributeJson.put("DeployStatus", deployStatus);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
                sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
                attributeJson.put("eventTime", sdf.format(new Date()));
                deviceDTO.setAttributeJson(attributeJson);
                deviceCentService.batchUpdate(null, Collections.singletonList(deviceDTO));
            }
        }

    }
}
